/*
 * Copyright 2008 Daniel Freitas
 *
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Army Creator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Army Creator.  If not, see <http://www.gnu.org/licenses/>.
 */
package cheesr.application;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.NumberValidator;
import org.apache.wicket.validation.validator.StringValidator;

import cheesr.model.Address;
import cheesr.model.Cart;

public class CheckOutPage extends CheesrPage {
    @SuppressWarnings("serial")
    public CheckOutPage() {
        Form form = new Form("form");
        add(form);
        Address address = getCart().getBillingAddress();
        form.setModel(new CompoundPropertyModel(address));
        TextField nameField = new TextField("name");
        nameField.add(StringValidator.lengthBetween(5, 32));
        nameField.setRequired(true);

        TextField zipCodeField = new TextField("zipcode");
        zipCodeField.setRequired(true);
        zipCodeField.add(NumberValidator.range(0, 99999));

        form.add(nameField);
        form.add(new TextField("street"));
        form.add(zipCodeField);
        form.add(new TextField("city"));

        final FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
        feedbackPanel.setOutputMarkupId(true);

        add(feedbackPanel);

        form.add(new Link("cancel") {
            @Override
            public void onClick() {
                setResponsePage(Index.class);
            }
        });

        form.add(new AjaxButton("order") {
            @Override
            protected void onError(AjaxRequestTarget target, Form form) {
                target.addComponent(feedbackPanel);
            }

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form form) {
                Cart cart = getCart();
                // Prints to make sure CompoundPropertyModel worked
                System.out.println(cart.getBillingAddress().getName());
                System.out.println(cart.getBillingAddress().getStreet());
                System.out.println(cart.getBillingAddress().getCity());
                System.out.println(cart.getBillingAddress().getZipcode());
                // charge customers' credit card
                // ship cheeses to our customer
                // clean out shopping cart
                cart.clearCart();
                cart.clearBillingAddress();
                // return to front page
                setResponsePage(Index.class);
            }
        }.setOutputMarkupId(true));
    }
}
